#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <Servo.h>

//=============  led flash ==================
Ticker flashTicker;
bool flashFlag = false;
int MaxTimes = -1;
int times = 0;
void closeLed(){
  digitalWrite(LED_BUILTIN, HIGH);
}

void openLed(){
  digitalWrite(LED_BUILTIN, LOW);
}
void stopFlash(){
  closeLed();
  flashTicker.detach();
  times = 0;
}


void flashLed(){

  if (MaxTimes != -1 && times >= MaxTimes){
    stopFlash();
    return;
  }

  if(flashFlag){
    closeLed();
  }else{
    openLed();
    times ++;
  }
  flashFlag = !flashFlag;
}

void beginFlash(int t){
  MaxTimes = t;
  times = 0;
  flashTicker.attach_ms(500, flashLed);
}

